package com.lws.rxutils.asynconce;

import android.view.View;

import androidx.annotation.CheckResult;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidDisposable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModel;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.disposables.DisposableContainer;
import io.reactivex.rxjava3.functions.Action;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class CompleteRxAsyncOnce {

    private final Completable completable;

    CompleteRxAsyncOnce(@NonNull Completable completable) {
        this.completable = completable.subscribeOn(Schedulers.io());
    }

    @CheckResult
    @NonNull
    public Disposable start(@NonNull Action onComplete) {
        return completable.observeOn(AndroidSchedulers.mainThread())
                .subscribe(onComplete);
    }

    @CheckResult
    @NonNull
    public Disposable start(@NonNull Action onComplete, @NonNull Consumer<Throwable> onError) {
        return completable.observeOn(AndroidSchedulers.mainThread())
                .subscribe(onComplete, onError);
    }


    public void start(@NonNull DisposableContainer container, @NonNull Action onComplete) {
        completable.observeOn(AndroidSchedulers.mainThread())
                .subscribe(onComplete, Debug::logOnThrowable, container);
    }

    public void start(@NonNull DisposableContainer container, @NonNull Action onComplete, @NonNull Consumer<Throwable> onError) {
        completable.observeOn(AndroidSchedulers.mainThread())
                .subscribe(onComplete, onError, container);
    }


    public void start(@NonNull ViewModel viewModel, @NonNull Action onComplete) {
        start(AndroidDisposable.from(viewModel), onComplete);
    }

    public void start(@NonNull ViewModel viewModel, @NonNull Action onComplete, @NonNull Consumer<Throwable> onError) {
        start(AndroidDisposable.from(viewModel), onComplete, onError);
    }


    public void start(@NonNull Lifecycle lifecycle, @NonNull Action onComplete) {
        start(AndroidDisposable.from(lifecycle), onComplete);
    }

    public void start(@NonNull Lifecycle lifecycle, @NonNull Action onComplete, @NonNull Consumer<Throwable> onError) {
        start(AndroidDisposable.from(lifecycle), onComplete, onError);
    }


    public void start(@NonNull LifecycleOwner lifecycleOwner, @NonNull Action onComplete) {
        start(AndroidDisposable.from(lifecycleOwner), onComplete);
    }

    public void start(@NonNull LifecycleOwner lifecycleOwner, @NonNull Action onComplete, @NonNull Consumer<Throwable> onError) {
        start(AndroidDisposable.from(lifecycleOwner), onComplete, onError);
    }


    public void start(@NonNull View view, @NonNull Action onComplete) {
        start(AndroidDisposable.from(view), onComplete);
    }

    public void start(@NonNull View view, @NonNull Action onComplete, @NonNull Consumer<Throwable> onError) {
        start(AndroidDisposable.from(view), onComplete, onError);
    }

}
